<?php


function upload_form_page(){
	$view = form_header( "upload" , "upload"  );
	
	
	$last_struktur = "SELECT * FROM pr_struktur_gaji_tahun ORDER BY struktur_gaji_id limit 1 ";
	$last_struktur_result = my_query($last_struktur);
	$row_last = my_fetch_array($last_struktur_result);
	$last_id = $row['struktur_gaji_id'];
	
	$query = "SELECT * FROM pr_struktur_gaji_tahun";
	$result = my_query($query);
	$struktur_opsi = array();
	while($row_struktur = my_fetch_array($result)){
		$struktur_opsi[$row_struktur['struktur_gaji_id']] = $row_struktur['nomor_sk'];  
	}
	
	$struktur_gaji = array(
		'name'=>'struktur_gaji_id',
		'value'=>$last_id,
		'id'=>'file' ,'size'=>'70',
		'type'=>'file'
	);
	
	$form_struktur_gaji = form_dropdown($struktur_gaji , $struktur_opsi );
	$view .= form_field_display( $form_struktur_gaji , "Strtuktur gaji by grade" );
	
	$file = array(
			'name'=>'filexl',
			'value'=>(isset($_POST['filexl'])? $_POST['filexl'] : $fields['filexl']),
			'id'=>'file' ,'size'=>'70',
			'type'=>'file'
	);
	$form_nominal = form_dynamic($file);
	$view .= form_field_display( $form_nominal , "File CSV" );
	
	$submit = array(
		'value' => ' Proses ',
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}


function upload_kenaikan_pa_csv(){
	$path 		= 'files/kenaikan/';
	$filename 	= strtolower($_FILES['filexl']['name']);
	$upload 	= move_uploaded_file($_FILES['filexl']['tmp_name'] , $path.$filename);
	
	if($upload){
		$t = 0;
		$handle = fopen($path.$filename, "r");
		while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
			stor_data_kenaikan_pa($data); 
		} 
		fclose($handle);
	}
	return true;
}

function auto_penilaian($karyawan_id , $skor , $penilaian_id){
		$total_nilai=$count_nilai=0;
		my_query("DELETE FROM penilaian_karyawan_evaluasi WHERE karyawan_id ={$_GET['karyawan_id']}");
		
		
		$query_penilain = "SELECT * FROM unsur_evaluasi WHERE penilaian_id = {$penilaian_id}";
		$result_penilaian = my_query($query_penilain);
		while($rp  = my_fetch_array($result_penilaian)){	
			$unsur_id = $rp['unsur_evaluasi_id'];
			$query = "SELECT evaluasi_id FROM evaluasi WHERE unsur_evaluasi_id = {$unsur_id}";
			$result = my_query($query);
			while($row = my_fetch_array($result)){
				if($skor =='A'){	
					$mix = rand(86,100);
				}elseif($skor =='B'){
					$mix = rand(73,85);
				}elseif($skor =='C'){
					$mix = rand(56,72);
				}elseif($skor =='D'){
					$mix = rand(45,56);
				}else{
					$mix = rand(10,44);
				}
				
				$datas = array(
					'karyawan_id'	=>my_type_data_int($karyawan_id),
					'evaluasi_id'	=>my_type_data_int($row['evaluasi_id']),
					'nilai'			=>my_type_data_str($mix),
				);
				my_insert_record( 'penilaian_karyawan_evaluasi' , $datas ); 
			}
		}
		
		if($skor =='A'){	
			$evaluasi_nilai_id = 5; 
		}elseif($skor =='B'){
			$evaluasi_nilai_id = 4; 
		}elseif($skor =='C'){
			$evaluasi_nilai_id = 3; 
		}elseif($skor =='D'){
			$evaluasi_nilai_id = 2; 
		}else{
			$evaluasi_nilai_id = 1; 
		}
		
		$datas2 = array(
			'karyawan_id' 		=> my_type_data_int($karyawan_id) ,
			'penilaian_id'		=> my_type_data_int($penilaian_id), 
			'evaluasi_nilai_id' => my_type_data_int($evaluasi_nilai_id) 
		);
		my_insert_record('penilaian_karyawan',$datas2  );
		
}


function already_entry($karyawan_id){
	$query = "SELECT karyawan_id FROM pr_kenaikan_perf_app WHERE karyawan_id = {$karyawan_id}";
	$result = my_query($query);
	if(my_num_rows($result) > 0){
		return true;
	}
	return false;
	
}
 

 function default_periode_id(){
	$query = "SELECT MAX(periode_id) as maxpid FROM pr_kenaikan_perf_app ";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return (int) $row['maxpid'];
 }
 
function stor_data_kenaikan_pa($data){

	ini_set("max_execution_time","10000");
	$nik = $data[0];
	$skor = $data[1];
	$struktur_gaji_id = $_POST['struktur_gaji_id'];
	
	$query = "SELECT b.karyawan_id , b.gapok_km, b.grade_id ,b.status_id FROM karyawan a 
	INNER JOIN kekaryawanan b ON a.karyawan_id = b.karyawan_id
	WHERE a.nik ='{$nik}'";
	$res = my_query($query);
	$row = my_fetch_array($res);
	
	
	$gaji_current = $row['gapok_km']; 	//DATA GAJI SEKARANG
	$karyawan_id = $row['karyawan_id'];		//DATA ID KARYAWAN YBS
	$grade_id = $row['grade_id'];		//DATA GRADE KARYAWAN YBS
	$status_id = $row['status_id'];		//DATA KONTRAK/BUKAN KARYAWAN YBS
	$periode_id = get_periode_gaji();
	
		
	if(already_entry($karyawan_id))return false;
	$max_struktur = "SELECT * 
		FROM  `struktur_gaji_detail` 
		WHERE struktur_gaji_id = '{$struktur_gaji_id}' AND grade_id = {$grade_id} ";
	$result = my_query($max_struktur);
	$row2 = my_fetch_array($result);

	$minimum = $row2['min'];		//DATA MINIMUM
	$middle = $row2['mid'];			//DATA TENGAH
	$maksimum = $row2['max'];			//DATA TENGAH
	
	//FAKTOR KALI DARI SKOR
	if($skor == 'A')$factor = 0.0400;	
	elseif($skor == 'B')$factor = 0.0250;
	elseif($skor == 'C')$factor = 0.0125;
	elseif($skor == 'D')$factor = 0.0030;
	else $factor = 0;
	
	if($status_id == '2'){ 
		$adj =$newGaji =   $gaji_current ;
	}else{
	
		$adj = $gaji_current ;
		$status = 0;
		if( $gaji_current < $minimum){ 	//CHECK JIKA KURANG DARI MINIMUM
			$adj = $minimum;
			$status = 1;
		}
		if( $gaji_current > $maksimum ){ 
			$adj = $maksimum;
		}	 
		$newGaji = 	( $middle * $factor ) + $adj;
		$newGaji = 	round( $newGaji );
		
	}	
	$ngapok =	round($newGaji * 1.0406);
	$datas = array(
		'karyawan_id'	=> my_type_data_str($karyawan_id),	
		'periode_id'	=> my_type_data_str($periode_id),	
		'grade_id'	=> my_type_data_str($grade_id),	
		'struktur_gaji_id'	=> my_type_data_str($struktur_gaji_id),			 
		'skor'	=> my_type_data_str($skor),		 	 	 	 	 	 	 
		'nfaktor'	=> my_type_data_str($factor),		 	 	 	 	 	 	 
		'datetime_added'	=> my_type_data_function('NOW()'),		 	 	 	 	 	 	 
		'adjust'	=> my_type_data_str($adj) ,
		'nadjust'	=> my_type_data_str($newGaji) ,
	);
	return my_insert_record('pr_kenaikan_perf_app',$datas); 
}
 
function get_tanggal_by_periode($periode_id){ 
	$periodes = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periodes['tanggal']));
	$tahun = $periodes['tahun'] ;
	return sprintf( '%02d',$bulan).'-'.$tahun ;
}
 function upload_result_page($pid){
 
	$header = array( 
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:40%;'),  
		'Grade'=>array('style'=>'border-bottom:2px solid;width:10%;text-align:center;'),  
		'PA'=>array('style'=>'border-bottom:2px solid;width:10%;text-align:center;'),  
		'Adj'=>array('style'=>'border-bottom:2px solid;width:15%;text-align:right;'),  
		'CGOS'=>array('style'=>'border-bottom:2px solid;width:15%;text-align:right;'), 
	);
	
	$query = "SELECT * , b.nik , c.nama ,d.grade_id FROM pr_kenaikan_perf_app a 
			INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
			INNER JOIN kekaryawanan d ON b.karyawan_id = d.karyawan_id
			INNER JOIN biodata c ON b.biodata_id = c.biodata_id WHERE periode_id = {$pid} ";
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =30;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array();   
	while( $ey = my_fetch_array($result) ){
		$row[] = array(
			'nik'=>$ey['nik'],
			'nama'=>$ey['nama'],
			'grade'=>position_text_align($ey['grade_id'],'center'),
			'pa'=>position_text_align($ey['skor'],'center'),
			'adj'=>position_text_align(rp_format( $ey['adjust']),'right'),
			'nadj'=>position_text_align(rp_format($ey['nadjust']) ,'right'),
		);
		$periode_id = $ey['periode_id'];
	}
	$datas = table_rows($row); 
	$menuontop = array(  
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=list_excel&pid='.$periode_id.'\''),
		'Upload PA'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=upload\''),
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	$paging = $kgPagerOBJ ->showPaging();
	
	$pilih_periode_opsi = array();
	$query_pilih = "SELECT * FROM pr_periode";
	$result_pilih = my_query($query_pilih);
	while($row_pilih = my_fetch_array($result_pilih)){
		$pilih_periode_opsi[$row_pilih['periode_id']] = date('m-Y',strtotime($row_pilih['tanggal']));
	}
	$pilih_periode = array(
		'name'=>'pid',
		'value'=>$pid,
	);
	$form_pilih_periode = form_dropdown($pilih_periode , $pilih_periode_opsi);
	$query_struktur = "SELECT  	a.struktur_gaji_id  ,b.nomor_sk FROM 	pr_kenaikan_perf_app a
INNER JOIN pr_struktur_gaji_tahun b ON a.struktur_gaji_id = b.struktur_gaji_id	
WHERE a.periode_id = {$pid} LIMIT 1";
	$result_struktur = my_query($query_struktur);
	$row_struktur = my_fetch_array($result_struktur);
	$struktur_nomor = strtoupper($row_struktur['nomor_sk']);
	$view  ='<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	$view .= form_field_display( '<span class="label_form"> '.$form_pilih_periode.'</span>'   , "<b>Periode Gaji</b>"    );
	$view .= form_field_display( '<span class="label_form"> '.$struktur_nomor.'</span>'   , "<b>Struktur gaji</b>"    );
	$view .='</table>';
	return $view .$printmenu .table_builder($header , $datas ,  9 , false ,$paging);
 }
 
 function report_excel_page($pid){
 	$header = array( 
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:40%;'),  
		'Grade'=>array('style'=>'border-bottom:2px solid;width:10%;text-align:center;'),  
		'PA'=>array('style'=>'border-bottom:2px solid;width:10%;text-align:center;'),  
		'Adj'=>array('style'=>'border-bottom:2px solid;width:15%;text-align:right;'),  
		'CGOS'=>array('style'=>'border-bottom:2px solid;width:15%;text-align:right;'), 
	);
	
	
	$query = "SELECT * , b.nik , c.nama ,d.grade_id FROM pr_kenaikan_perf_app a 
			INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
			INNER JOIN kekaryawanan d ON b.karyawan_id = d.karyawan_id
			INNER JOIN biodata c ON b.biodata_id = c.biodata_id WHERE periode_id = {$pid} ";
	$result = my_query($query);
	$row = array();
	while( $ey = my_fetch_array($result) ){
		$row[] = array(
			'nik'=>"'".sprintf('%07d' , $ey['nik']),
			'nama'=>$ey['nama'],
			'grade'=>position_text_align($ey['grade_id'],'center'),
			'pa'=>position_text_align($ey['skor'],'center'),
			'adj'=>position_text_align(rp_format( $ey['adjust']),'right'),
			'nadj'=>position_text_align(rp_format($ey['nadjust']) ,'right'),
		); 
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 6 ,false ); 
 }